#include "TViewDraw.h"
#include "vhc/constview4fun.h"
#include "vhc/constview4id.h"

#include <QPainterPath>
#include <QPoint>
#include <QPointF>
#include <QtTypes>

#include <QDebug>

#include "IdRootMenu.h"




namespace kepen {
namespace core {


void TViewDraw::DrawCheckedTriFlag(QPainter *painter, int left, int top, int width,int height, quint32 boolFlag)
{



    bool isDeact = BoolFlagDeactivate == (BoolFlagDeactivate & boolFlag);
    bool isOver = BoolFlagMouseOver == (BoolFlagMouseOver & boolFlag);
    bool isTouched = BoolFlagTouched == (BoolFlagTouched & boolFlag);

    quint32 colorFlag ;
    qreal thickness = 1.8f ;
    qreal _widthTri = 14 , _heightTri = 14 ;
    qreal _rightTri = left + _widthTri;
    qreal _bottomTri = top + _heightTri;
    qreal fLeft,fTop,fWidth,fHeight;

    if(isDeact){
        colorFlag =  Checked_Flag_Deactivate;
    }else{
        if(isTouched){
            colorFlag =  Checked_FlagColor_Touched_in;
        }else if(isOver){
            colorFlag =  Checked_FlagColor_Over;
        }else{
            colorFlag =  Checked_FlagColor;
        }

    }
    QBrush _brushTri(QColor(QRgba64::fromArgb32(colorFlag)),Qt::SolidPattern);
    QPen pen(_brushTri,thickness, Qt::SolidLine);


    QPainterPath _path;

    fLeft = left ;
    fTop = top + 1;
    fWidth = width ;
    fHeight = height - thickness ;


    _path.moveTo(left, top);
    _path.lineTo(_rightTri, top);
    _path.lineTo(left, _bottomTri);
    _path.lineTo(left, top);
    _path.closeSubpath();

    painter->fillPath(_path,_brushTri);

    painter->setBrush(Qt::BrushStyle::NoBrush);
    painter->setPen(pen);
    painter->drawRect(fLeft,fTop,fWidth,fHeight);

}


void TViewDraw::DrawShapePickPoint(QPainter *painter, int left, int top, int width,int height,quint32 boolFlag)
{

    DefineColorTxtR2();
    int density = PixDIP;
    int y0 = top + (height >> 1) ;
    int x0 = left + (width >> 1);

    float thickness = width_border_default * density;
    int rx = 10 * density, ry = 8 * density, nLeft, nTop, nRight, nBottom, nHeight2, nWidth2;
    int nHeight_left = 4 * density;
    int nSize_x = 4 * density;
    int nSize_y = 4 * density;
    int rxx = nSize_x << 1;
    int ryy = nSize_y << 1;
    int x, y;

    QBrush brushFill(QColor(QRgba64::fromArgb32(focusColor)),Qt::SolidPattern);
    QBrush brushBorder(QColor(QRgba64::fromArgb32(textColor)),Qt::SolidPattern);
    QPen pen(brushBorder,thickness, Qt::SolidLine);

    QPainterPath path;

    nHeight2 = 6 * density;
    nWidth2 = 8 * density;

    nLeft = x0 - rx;
    nRight = x0 + rx;
    nTop = y0 - ry;
    nBottom = y0 + ry + 2.4f*density;

    y = nBottom - nHeight2;
    x = nRight - nWidth2;

    path.moveTo(nLeft, nTop);
    path.lineTo(nRight, nTop);

    path.lineTo(nRight, y);

    path.lineTo(x, nBottom);
    path.lineTo(nLeft, nBottom);
    path.lineTo(nLeft, nTop);
    path.closeSubpath();

    painter->setBrush(Qt::BrushStyle::NoBrush);
    painter->setPen(pen);
    painter->drawPath(path);


    nLeft = nLeft - nSize_x;
    nTop = nTop + nHeight_left;
    // nRight = nLeft + rxx;
    // nBottom = nTop + ryy;

    painter->fillRect(nLeft, nTop, rxx, ryy, brushFill);



}

void TViewDraw::DrawShapePickLine(QPainter *painter, int left, int top, int width, int height,quint32 boolFlag)
{
    DefineColorTxtR2();


    int density = PixDIP;
    int y0 = top + (height >> 1);
    int x0 = left + (width >> 1);

    float thickness = width_border_default * density;
    int rx = 10 * density, ry = 8 * density, nLeft, nTop, nRight, nBottom, nHeight2, nWidth2;
    int x, y;
    QBrush brushBorder(QColor(QRgba64::fromArgb32(textColor)),Qt::SolidPattern);
    QPen pen(brushBorder,thickness, Qt::SolidLine);
    QPainterPath path;

    nHeight2 = 6 * density;
    nWidth2 = 8 * density;
    nLeft = x0 - rx;
    nRight = x0 + rx+density;
    nTop = y0 - ry;
    nBottom = y0 + ry+ 2*density;
    y = nBottom - nHeight2;
    x = nRight - nWidth2;

    path.moveTo(nLeft, nTop);
    path.lineTo(nRight, nTop);

    path.lineTo(nRight, y);

    path.lineTo(x, nBottom);
    path.lineTo(nLeft, nBottom);
    path.lineTo(nLeft, nTop);
    path.closeSubpath();

    painter->setBrush(Qt::BrushStyle::NoBrush);
    painter->setPen(pen);
    painter->drawPath(path);


    thickness = 4 * density;

    int x1,y1,x2,y2;

    x1 = x -density;
    y1 = nBottom - (9 * density);
    x2 = x1;
    y2 = nBottom + (density);

    QBrush brushLine(QColor(QRgba64::fromArgb32(focusColor)),Qt::SolidPattern);
    QPen penLine(brushLine,thickness, Qt::SolidLine);
    painter->setBrush(Qt::BrushStyle::NoBrush);
    painter->setPen(penLine);

    painter->drawLine(x1, y1, x2, y2);
}

void TViewDraw::DrawShapePolygonR2(QPainter *painter, int x, int y, quint32 boolFlag)
{

    float  outLine []= { 10.5f,7.0f,7.5f,-9.0f,-4.5f,-9.0f,-10.5f,-1.0f,-3.5f,9.0f,10.5f,7.0f };
    int density = PixDIP;
    int i ,j;

    int N = sizeof(outLine)/sizeof(outLine[0]);

    DefineColorTxt();
    float thickness = width_border_default * density;
    float x0, y0, factor =  density;

    QBrush brushBorder(QColor(QRgba64::fromArgb32(textColor)),Qt::SolidPattern);
    QPen penLine(brushBorder,thickness, Qt::SolidLine);

    QPainterPath path;
    for (i = 0; i < N; ++i)
        outLine[i] = factor * outLine[i];
    x0 = x + outLine[0];
    y0 = y + outLine[1];
    path.moveTo(x0, y0);

    N >>= 1;
    j =1;
    for (i = 1; i < N; ++i) {
        x0 = x + outLine[++j];
        y0 = y + outLine[++j];
        path.lineTo(x0, y0);
    }
    path.closeSubpath();

    painter->setBrush(Qt::BrushStyle::NoBrush);
    painter->setPen(penLine);
    painter->drawPath(path);

}

void TViewDraw::DrawShapePolygon(QPainter *painter, int x, int y,quint32 boolFlag)
{
    float  outLine []= { 10.5f,7.0f,7.5f,-9.0f,-4.5f,-9.0f,-10.5f,-1.0f,-3.5f,9.0f,10.5f,7.0f };
    float ring [] =   { 7.77f,5.18f,5.55f,-6.66f,-3.33f,-6.66f,-7.77f,-0.74f,-2.59f,6.66f,7.77f,5.18f };
    bool isChecked = BoolFlagChecked == (BoolFlagChecked & boolFlag);
    bool isMouseOver = BoolFlagMouseOver == (BoolFlagMouseOver & boolFlag);
    bool isDeAct = false == isMouseOver && BoolFlagDeactivate == (BoolFlagDeactivate & boolFlag);
    quint32 fillColor = isDeAct ? Color_Text_Deactivate:(isChecked ?  0xffd2d2d2U:0xff686868U);
    quint32 textColor = isDeAct ? Color_Text_Deactivate:(isChecked ?  Color_Font_ToolBar_checked:Color_Font_ToolBar);

    int i ,j;

    int nLengthOut = sizeof(outLine)/sizeof(outLine[0]);
    int nLengthRing = sizeof(ring)/sizeof(ring[0]);

    float thickness = width_border_default ;
    float x0, y0;
    QBrush brushFill(QColor(QRgba64::fromArgb32(fillColor)),Qt::SolidPattern);
    QBrush brushBorder(QColor(QRgba64::fromArgb32(textColor)),Qt::SolidPattern);
    QPen penLine(brushBorder,thickness, Qt::SolidLine);

    QPainterPath path;
    QPainterPath pathRng;
    for (i = 0; i < nLengthOut; ++i)
        outLine[i] =  outLine[i];

    for (i = 0; i < nLengthRing; ++i)
        ring[i] =  ring[i];

    x0 = x + outLine[0];
    y0 = y + outLine[1];
    path.moveTo(x0, y0);
    nLengthOut >>= 1;
    j =1;
    for (i = 1; i < nLengthOut; ++i) {
        x0 = x + outLine[++j];
        y0 = y + outLine[++j];
        path.lineTo(x0, y0);
    }
    path.closeSubpath();


    x0 = x + ring[0];
    y0 = y + ring[1];
    pathRng.moveTo(x0, y0);
    nLengthRing >>= 1;
    j = 1;
    for (i = 1; i < nLengthRing; ++i) {
        x0 = x + ring[++j];
        y0 = y + ring[++j];
        pathRng.lineTo(x0, y0);
    }
    pathRng.closeSubpath();

    // path.addPath(pathRng);

    painter->setBrush(Qt::BrushStyle::NoBrush);
    painter->setPen(penLine);
    painter->drawPath(path);


    painter->fillPath(pathRng,brushFill);
}

void TViewDraw::DrawMakeHolePolygon(QPainter *painter, int x, int y,quint32 boolFlag)
{
    bool isMouseOver = BoolFlagMouseOver == (BoolFlagMouseOver & boolFlag);
    bool isDeAct = BoolFlagDeactivate == (BoolFlagDeactivate & boolFlag);
    bool isChecked = BoolFlagChecked == (BoolFlagChecked & boolFlag);
    quint32 textColor = isChecked ?  Color_Font_ToolBar_checked:Color_Font_ToolBar;
    quint32 focusColor = isChecked ? Checked_Background:0xffe8e8e8U;
    int density = PixDIP;
    float thickness = width_border_default * density;
    int dd = isChecked ?7:6;
    int r1 = dd * density;
    int sizeRect = 10 * density;
    int sizeMark ;
    int left, top;
    if(false == isMouseOver && isDeAct){
        textColor = Color_Text_Deactivate;
        focusColor = 0xffd8d8d8U;
    }
    QBrush brushBack(QColor(QRgba64::fromArgb32(focusColor)),Qt::SolidPattern);
    QBrush brushBorder(QColor(QRgba64::fromArgb32(textColor)),Qt::SolidPattern);
    QPen penLine(brushBorder,thickness, Qt::SolidLine);




    left = x - sizeRect;
    top = y - sizeRect;
    sizeMark = sizeRect << 1;
    if(isChecked)
    {
        painter->setBrush(brushBorder);

    }
    else{
        painter->setBrush(Qt::BrushStyle::NoBrush);
    }

    painter->setPen(penLine);
    painter->drawRect(left,top,sizeMark,sizeMark);


    left = x - r1;
    top = y - r1;
    sizeMark = r1 <<1;

    if(isDeAct){
        if(isChecked){
            painter->setPen(Qt::PenStyle::NoPen);
            painter->setBrush(brushBack);
            painter->drawEllipse(left,top,sizeMark,sizeMark);
        }else{
            painter->setPen(penLine);
            painter->setBrush(Qt::BrushStyle::NoBrush);
            painter->drawEllipse(left,top,sizeMark,sizeMark);
        }

    }else{
        if(isChecked)
        {
            painter->setPen(Qt::PenStyle::NoPen);
            painter->setBrush(brushBack);
        }
        else{
            painter->setPen(penLine);
            painter->setBrush(Qt::BrushStyle::NoBrush);
        }

        painter->drawEllipse(left,top,sizeMark,sizeMark);
    }




}

void TViewDraw::DrawEditToolVector(QPainter *painter, int x, int y)
{
    float  outLine []= { 9.08f,0.41f,6.28f,1.99f,5.14f,1.99f,4.00f,2.84f,3.72f,3.98f,1.73f,6.82f,-2.25f,8.24f,
                       -6.24f,6.82f,-4.50f,5.69f,0.00f,4.50f,-9.08f,-5.98f,-9.08f,-6.54f,-8.79f,-7.40f,-8.23f,-8.24f,
                       -7.09f,-8.24f,0.02f,0.00f,1.16f,0.57f,2.58f,0.29f,3.44f,-0.57f,5.40f,-3.60f,9.08f,0.41f };
    int density = PixDIP;
    int i ,j;

    int N = sizeof(outLine)/sizeof(outLine[0]);

    float thickness = width_border_default * density;
    float x0, y0, factor =  density;
    QBrush brushBorder(QColor(QRgba64::fromArgb32(Color_Font_ToolBar)),Qt::SolidPattern);
    QPen penLine(brushBorder,thickness, Qt::SolidLine);

    QPainterPath path;
    for (i = 0; i < N; ++i)
        outLine[i] = factor * outLine[i];
    x0 = x + outLine[0];
    y0 = y + outLine[1];
    path.moveTo(x0, y0);

    N >>= 1;
    j =1;
    for (i = 1; i < N; ++i) {
        x0 = x + outLine[++j];
        y0 = y + outLine[++j];
        path.lineTo(x0, y0);
    }
    path.closeSubpath();

    painter->setBrush(Qt::BrushStyle::NoBrush);
    painter->setPen(penLine);
    painter->drawPath(path);
}

void TViewDraw::DrawShapeCoords(QPainter *painter,quint32 boolFlag, float x, float y, size_t length, float *coords)
{
    DefineColorTxt();

    float density = PixDIP;
    size_t i ,j ,numPts = length >> 1;

    float thickness = width_border_default * density;
    float x0, y0, factor =  density;
    QBrush brushBorder(QColor(QRgba64::fromArgb32(textColor)),Qt::SolidPattern);
    QPen penLine(brushBorder,thickness, Qt::SolidLine);

    QPainterPath path;
    for (i = 0; i < length; ++i)
        coords[i] = factor * coords[i];

    x0 = x + coords[0];
    y0 = y + coords[1];
    path.moveTo(x0, y0);

    j =1;
    for (i = 1; i < numPts; ++i) {
        x0 = x + coords[++j];
        y0 = y + coords[++j];
        path.lineTo(x0, y0);
    }
    path.closeSubpath();

    painter->setBrush(Qt::BrushStyle::NoBrush);
    painter->setPen(penLine);
    painter->drawPath(path);
}

void TViewDraw::DrawShapeCircle(QPainter *painter, int x, int y, int radius, quint32 boolFlag)
{
    DefineColorTxt();
    int left, top,R = radius<<1;
    float thickness = width_border_defaultR2 ;

    left = x - radius;
    top = y - radius;
    QBrush brushBorder(QColor(QRgba64::fromArgb32(textColor)),Qt::SolidPattern);
    QPen penLine(brushBorder,thickness, Qt::SolidLine);
    painter->setPen(penLine);
    painter->setBrush(Qt::BrushStyle::NoBrush);


    painter->drawEllipse(left,top,R,R);

}

void TViewDraw::DrawComboxFlag(QPainter *painter, int x, int y, quint32 boolFlag)
{
    DefineColorTxt();

    int left,top,right,bottom,h=3,w=4;

    QPainterPath path;

    left = x - w;
    right = x + w;
    top = y - h;
    bottom = y+ h;
    path.moveTo(left,top);
    path.lineTo(right,top);
    path.lineTo(x,bottom);
    path.lineTo(left,top);
    path.closeSubpath();
    painter->fillPath(path,QColor(QRgba64::fromArgb32(textColor)));

}

void TViewDraw::DrawBtnMinimize(QPainter *painter, int x, int y, int radius, quint32 boolFlag)
{
    DefineColorTxt();
    int left, right;
    float thickness = width_border_defaultR2 ;

    left = x - radius;
    right = x + radius;
    QBrush brushBorder(QColor(QRgba64::fromArgb32(textColor)),Qt::SolidPattern);
    QPen penLine(brushBorder,thickness, Qt::SolidLine);
    painter->setPen(penLine);
    painter->setBrush(Qt::BrushStyle::NoBrush);


    painter->drawLine(left,y,right,y);
}

void TViewDraw::DrawBtnClose(QPainter *painter, int x, int y, int radius, quint32 boolFlag)
{
    DefineColorTxt();
    int left, right,top,bottom;
    float thickness = width_border_defaultR2 ;

    left = x - radius;
    right = x + radius;
    top = y- radius;
    bottom = y + radius;
    QBrush brushBorder(QColor(QRgba64::fromArgb32(textColor)),Qt::SolidPattern);
    QPen penLine(brushBorder,thickness, Qt::SolidLine);
    painter->setPen(penLine);
    painter->setBrush(Qt::BrushStyle::NoBrush);


    painter->drawLine(left,top,right,bottom);
    painter->drawLine(left,bottom,right,top);
}

void TViewDraw::DrawLabelIcon(QPainter *painter, int left, int y, int id, quint32 boolFlag)
{

}


}
}
